/******************* 公共变量 *******************/
var lazy,
	loadJudge = true,
	sortIndex,
	typeIndex,
	sexIndex,
	sex,
	lng,
	lat,
	themeIndex;

/******************* 数据载入 *******************/
$.ajax({
	url: checkNews,
	type: 'post',
	success: function(data) {
		console.log(data);

		common.hintNewMsg(data);
	}
});

$('html body .wrap .index .activity-type span, html body .wrap .index .activity-type a').on('tap', function () {
	$(this).addClass('active').siblings().removeClass('active');
});

/******************* 初始化微信 *******************/
wx.ready(function () {
	// alert('ready');
	wx.checkJsApi({  
		jsApiList : ['getLocation'],  
		success: function(res) {
		},
		fail: function (data) {
		},
		complete: function (data) {
			if (data.checkResult.getLocation == 'no') {
				useGps(true);
			} else {
				useGps();
			}
		}
	});

});

function useGps(useBaidu) {
	var bdMap = core.bdMap({
		// selector: 'map',
		// device: 'mb',
		// map: true,
		gpsDetail: function (data) {
			$('html body .header-title h1').html(data.result.addressComponent.city);
			lng = data.result.location.lng;
			lat = data.result.location.lat;
			$.ajax({
				url: siteUrl,
				type: 'post',
				data: {
					lng: data.result.location.lng,
					lat: data.result.location.lat,
					city: data.result.addressComponent.city
				},
				success: function (data) {
					console.log(data)

					lazy = ajax.lazy({
						device: 'mobile',
						// refreshHeight: 4 * rem,
						con: ['html body .wrap .index .activity-box'],
						win: ['html body .wrap'],
						winHeight: [winHeight],
						data: [function (num) {
							return {
								page: num,
								lng: lng,
								lat: lat
							}
						}],
						// pageLoading: true,
						// lazyLoading: true,
						refresh: false,
						imgloading: true,
						noData: 'body .no-data',
						type: 'post',
						url: [function (num) {
							return indexUrl;
						}],
						prop: 'list',
						cycle: [{
							list: {
								eleFun: function (data) {
									console.log(data)
									var cateStr = '';

									for (var i = 0; i < data.theme.length; i++) {
										if (data.theme[i] == 1) {
											// if (data.drink == 0) {
											// 	cateStr += '<span class="cate">' + 
											// 		'美食' + 
											// 	'</span>';
											// } else if (data.drink == 1) {
											// 	cateStr += '<span class="cate">' + 
											// 		'美食小酌' + 
											// 	'</span>';
											// }
											cateStr += '<span class="cate">' + 
												'美食小酌' + 
											'</span>';
										} else if (data.theme[i] == 2) {
											cateStr += '<span class="ktv">' + 
												'K<br>歌' + 
											'</span>';
										} else if (data.theme[i] == 3) {
											cateStr += '<span class="bar">' + 
												'酒吧' + 
											'</span>';
										}
									}
									return '<a href="' + meetingPage + '?cate_id=' + data.id + '" title="">' + 
										'<dl>' + 
											'<dt>' + 
												'<img src="' + data.aphoto_id + '" alt="" title="">' + 
												'<div><span>' + core.decimalRound(Number(data.total_money), 0) + '元夜生活红包，速抢！</span></div>' + 
											'</dt>' + 
											'<dd class="theme clearfix">' + 
												cateStr + 
											'</dd>' + 
											'<dd class="detail">' + 
												'<b>' + 
													'<img src="' + data.photo_id + '" alt="" title="" class="' + ((data.type == 0) ? 'blur' : '') + '">' + 
												'</b>' + 
												'<span>' + data.start_time + '</span>' + 
												'<strong>' + data.str_status + '</strong>' + 
												'<em>' + data.site + '</em>' + 
												'<i>'+ data.distance + 'km</i>' + 
											'</dd>' + 
										'</dl>' + 
									'</a>';
								}
							}
						}],
						callback: function (data) {
							if (loadJudge) {
								ajax.cycle({
									photos: {
										ele: 'body .subject-banner .swiper-wrapper',
										eleFun: function (data) {
											return '<div class="swiper-slide">' + 
												'<span href="' + data.url + '" title="">' + 
													'<img src="' + data.photo_id + '" alt="" title="">' + 
												'</span>' + 
											'</div>';
										}
									}
								}, data);

								/******************* 头部banner滚动 *******************/
								var bannerSwiper = new Swiper ('.subject-banner', {
									direction: 'horizontal',
									autoplay: 4000,
									// 如果需要分页器
									pagination: '.swiper-pagination',
									paginationClickable: true
								});
								loadJudge = false;

								$('body .subject-banner .swiper-wrapper').on('tap', 'span', function () {
									if (!bannerSwiper.animating) {
										core.href($(this).attr('href'));
									}
								});
							}
						}
					});

					// ajax.load({
					// 	url: indexUrl,
					// 	type: 'post',
					// 	cycle: {
					// 		photos: {
					// 			ele: 'body .subject-banner .swiper-wrapper',
					// 			eleFun: function (data) {
					// 				return '<div class="swiper-slide">' + 
					// 					'<a href="' + data.url + '" title="">' + 
					// 						'<img src="' + data.photo_id + '" alt="" title="">' + 
					// 					'</a>' + 
					// 				'</div>';
					// 			}
					// 		}
					// 	},
					// 	callback: function (data) {
					// 		console.log(data);
					// 		/******************* 头部banner滚动 *******************/
					// 		var bannerSwiper = new Swiper ('.subject-banner', {
					// 			direction: 'horizontal',
					// 			autoplay: 4000,
					// 			// 如果需要分页器
					// 			pagination: '.swiper-pagination',
					// 			paginationClickable: true
					// 		});
					// 	}
					// });
				}
			});
		}
	}, useBaidu);

	/******************* 搜索弹窗 *******************/
	$('html body .header-title .search').on('tap', function () {
		layer.open({
			content: '<h2>请选择筛选条件</h2>\
			<strong></strong>\
			<div class="sort">\
				<h3>排序</h3>\
				<div class="con">\
					<span><i></i>最新发布</span>\
					<span><i></i>离我最近</span>\
				</div>\
			</div>\
			<div class="classify">\
				<h3>类别</h3>\
				<div class="con">\
					<span><i></i>不限</span>\
					<span><i></i>约会</span>\
					<span><i></i>聚会</span>\
				</div>\
			</div>\
			<div class="sex">\
				<h3>性别</h3>\
				<div class="con">\
					<span sex="0"><i></i>不限</span>\
					<span sex="2"><i></i>女</span>\
					<span sex="1"><i></i>男</span>\
				</div>\
			</div>\
			<div class="theme">\
				<h3>主题</h3>\
				<div class="con">\
					<span><i></i>不限</span>\
					<span><i></i>美食</span>\
					<span><i></i>K歌</span>\
					<span><i></i>酒吧</span>\
				</div>\
			</div>\
			<div class="btn">\
				<span>确定</span>\
				<em>重置条件</em>\
			</div>',
			className: 'index-search',
			shadeClose: false,
			success: function ($ele) {
				// 关闭按钮
				$('body .layui-m-layer .layui-m-layermain .layui-m-layersection .index-search strong, .layui-m-layershade').on('tap', function () {
					layer.closeAll();
				});
				// 排序选择
				var sort = core.tabSwitch({
					tit: 'body .layui-m-layer .layui-m-layermain .layui-m-layersection .index-search .sort .con span',
					event: 'tap',
					active: sortIndex
				});
				// 类别选择
				var classify = core.tabSwitch({
					tit: 'body .layui-m-layer .layui-m-layermain .layui-m-layersection .index-search .classify .con span',
					event: 'tap',
					active: typeIndex
				});
				// 性别选择
				var sex = core.tabSwitch({
					tit: 'body .layui-m-layer .layui-m-layermain .layui-m-layersection .index-search .sex .con span',
					event: 'tap',
					active: sexIndex
				});
				// 主题选择
				var theme = core.tabSwitch({
					tit: 'body .layui-m-layer .layui-m-layermain .layui-m-layersection .index-search .theme .con span',
					event: 'tap',
					active: themeIndex
				});
				// 点击重置按钮
				$('body .layui-m-layer .layui-m-layermain .layui-m-layersection .index-search .btn em').on('tap', function () {
					sort.change(0);
					classify.change(0);
					sex.change(0);
					theme.change(0);
				});
				// 确定按钮
				$('body .layui-m-layer .layui-m-layermain .layui-m-layersection .index-search .btn span').on('tap', function () {
					sortIndex = $('body .layui-m-layer .layui-m-layermain .layui-m-layersection .index-search .sort .con .active').index();
					typeIndex = $('body .layui-m-layer .layui-m-layermain .layui-m-layersection .index-search .classify .con .active').index();
					sexIndex = $('body .layui-m-layer .layui-m-layermain .layui-m-layersection .index-search .sex .con .active').index();
					sex = $('body .layui-m-layer .layui-m-layermain .layui-m-layersection .index-search .sex .con .active').attr('sex');
					themeIndex = $('body .layui-m-layer .layui-m-layermain .layui-m-layersection .index-search .theme .con .active').index();
					lazy.tab(0, {
						url: function(num) {
							return searchUrl;
						},
						data: function (num) {
							return {
								page: num,
								type: sortIndex + 1,
								cate: typeIndex,
								sex: sex,
								theme: themeIndex,
								lng: lng,
								lat: lat
							}
						}
					});
					layer.closeAll();
				});
			}
		});
	});
}
